home *** CD-ROM | disk | FTP | other *** search
/ The Atari Compendium / The Atari Compendium (Toad Computers) (1994).iso / files / prgtools / mint / mgr / sparcmgr / demo1.zoo / demo / icon / iconmail.c < prev    next >
Encoding:
C/C++ Source or Header  |  1989-09-25  |  8.6 KB  |  358 lines

  1. /*                        Copyright (c) 1987 Bellcore
  2.  *                            All Rights Reserved
  3.  *       Permission is granted to copy or use this program, EXCEPT that it
  4.  *       may not be sold for profit, the copyright notice must be reproduced
  5.  *       on copies, and credit should be given to Bellcore where it is due.
  6.  *       BELLCORE MAKES NO WARRANTY AND ACCEPTS NO LIABILITY FOR THIS PROGRAM.
  7.  */
  8. /*    $Header: iconmail.c,v 4.2 88/06/30 16:21:42 bianchi rel4-2 $
  9.     $Source: /fs/16/21272/bianchi/src/nmgr/demo/icon/RCS/iconmail.c,v $
  10. */
  11. static char    RCSid_[] = "$Source: /fs/16/21272/bianchi/src/nmgr/demo/icon/RCS/iconmail.c,v $$Revision: 4.2 $";
  12.  
  13. /* check for new mail  (icon version) */
  14.  
  15. #include <sys/types.h>
  16. #include <sys/stat.h>
  17. #include <sys/time.h>        /* for fsleep */
  18. #include <signal.h>
  19. #include <sgtty.h>
  20. #include <stdio.h>
  21.  
  22. #include <term.h>
  23. #include "mail_icons.h"
  24.  
  25. #define MSG_READING    "\freading mail ...\r"
  26. #define MSG_CHECKING    "\rChecking for new mail..."
  27.  
  28. #define MAILF        "/usr/spool/mail"    /* spool file */
  29. #define MAIL        "mail"            /* name of mail command */
  30. #define POLL        60            /* polling interval */
  31. #define XPOS        240            /* x start of mail window */
  32. #define YPOS        190            /* y start of mail window */
  33. #define W_WIDE        80            /* width of mail window */
  34. #define W_HIGH        24            /* height of mail window */
  35. #define MAX_ICON    64            /* max icon size */
  36.  
  37. #define PROCESSED    2            /* new mail already processed */
  38.  
  39. #define S(x)            statb.x
  40. #define Isflag(arg,flag)    (!strncmp(arg,flag,strlen(flag)))
  41. #define Max(x,y)        ((x)>(y)?(x):(y))
  42. #define dprintf            if(debug) fprintf
  43.  
  44. #define fsleep() \
  45.    { \
  46.    struct timeval time; \
  47.    time.tv_sec = 0; \
  48.    time.tv_usec = 330000; \
  49.    select(0,0,0,0,&time); \
  50.    }
  51.  
  52. #define MENU_COUNT        (sizeof(menu)/sizeof(struct menu_entry))
  53.  
  54. struct menu_entry menu[] = {
  55.     "print","t\r",
  56.     "delete","dt\r",
  57.     "next","n\r",
  58.     "quit","q\r",
  59.     "help","?\r",
  60.     "headers","h *\r",
  61.     "abort","x\r",
  62. };
  63.  
  64. static struct    stat statb;    /* spool file status */
  65. static char    mail[255];    /* spool file path name */
  66. static long    omtime=0l;    /* previous file mod. time */
  67. static int    state = 0;    /* mail & window state */
  68. static int    poll = POLL;    /* poll interval */
  69. static int    debug=0;    /* for mgrmail -d >& /dev/tty?? */
  70. static int    x,y;        /* window position */
  71. static int    w,h;        /* window size */
  72. static int    border;        /* size of mgr border */
  73. static int    local=0;    /* use local icon only */
  74. static int    local_mode = -1;/* local mode bits for tty */
  75. static int    cwide, chigh;    /* width and height of font characters */
  76. static char    *termcap;
  77.  
  78. main(argc,argv)
  79.     char **argv;
  80. {
  81.     register int i;
  82.     int xpos = XPOS;        /* screen position of mail subwindow */
  83.     int ypos = YPOS;
  84.     int font = -1;            /* font to use for mail subwindow */
  85.     int shape = 1;            /* initially reshape window */
  86.     char *command = MAIL;        /* name of readmail command */
  87.  
  88.     char *getenv();
  89.     char *user = getenv("USER");
  90.     char line[MAXLINE];        /* event input buffer */
  91.  
  92.     int clean(), update();
  93.  
  94.     /* make sure environment is ok */
  95.  
  96.     ckmgrterm( *argv );
  97.  
  98.     if (user==NULL || *user=='\0') {
  99.         fprintf(stderr, "%s: No USER environment variable value.\n",
  100.             argv[0]);
  101.         exit(2);
  102.     }
  103.  
  104.     /* process arguments */
  105.  
  106.     for(i=1;i<argc;i++) {
  107.         if (Isflag(argv[i],"-s"))
  108.             shape = 0;
  109.         else if (Isflag(argv[i],"-d"))
  110.             debug = 1;
  111.         else if (Isflag(argv[i],"-l"))
  112.             local = 1;
  113.         else if (Isflag(argv[i],"-x"))
  114.             xpos = atoi(argv[i]+2);
  115.         else if (Isflag(argv[i],"-y"))
  116.             ypos = atoi(argv[i]+2);
  117.         else if (Isflag(argv[i],"-f"))
  118.             font = atoi(argv[i]+2);
  119.         else if (Isflag(argv[i],"-p"))
  120.             poll  = Max(atoi(argv[i]+2),10);
  121.         else if (Isflag(argv[i],"-M"))
  122.             command  = argv[i]+2;
  123.         else
  124.             usage(argv[0],argv[i]);
  125.     }
  126.     sprintf(mail,"%s/%s",MAILF,user);
  127.  
  128.     /* set up window environment */
  129.  
  130.     m_setup(M_FLUSH);
  131.     m_push(P_MENU|P_BITMAP|P_FONT|P_EVENT|P_FLAGS|P_POSITION);
  132.     if (font < 0)
  133.         font = 0;
  134.     m_font(font);
  135.     get_font( &cwide, &chigh );
  136.  
  137.     signal(SIGHUP,clean);
  138.     signal(SIGTERM,clean);
  139.     signal(SIGINT,clean);
  140.     signal(SIGALRM,update);
  141.  
  142.     dprintf(stderr,"pushing environment\n"); fflush(stderr);
  143.     m_ttyset();
  144.     m_setmode(M_NOWRAP);
  145.     m_setmode(M_ABS);
  146.     m_func(B_COPY);
  147.  
  148.     download_icon(&mbox_closed,1);
  149.     download_icon(&mbox_full,2);
  150.     download_icon(&mbox_zip,5);
  151.     download_icon(&mbox_open,6);
  152.  
  153.     get_size(&x,&y,&w,&h);
  154.         get_param(0,0,0,&border);
  155.     m_movecursor(x+30,0);
  156.  
  157.     m_setmode(M_ACTIVATE);
  158.     if (shape) {
  159.         m_shapewindow(x,y,2*border+MAX_ICON, 2*border+MAX_ICON);
  160.                 get_size(&x,&y,&w,&h);
  161.         }
  162.  
  163.     m_setevent(ACTIVATE,"A\r");
  164.     m_setevent(REDRAW,"R\r");
  165.  
  166.     m_clearmode(M_ACTIVATE);
  167.         set_icon(mbox_closed);
  168.  
  169.     dprintf(stderr,"Starting state 0x%x\n",state); fflush(stderr);
  170.  
  171.     update();
  172.  
  173.     termcap = getenv("TERMCAP");
  174.     if( termcap )
  175.         *termcap = '\0';
  176.  
  177.     /* wait for an event */
  178.  
  179.     while(1) {
  180.         if( m_gets(line) == NULL ) {
  181.             clearerr( m_termin );
  182.             continue;
  183.         }
  184.         dprintf(stderr,"state 0x%x line : %c\n",state,*line);
  185.         fflush(stderr);
  186.         switch(*line) {
  187.         case 'A':    /* window is activated */
  188.             if (!stat(mail,&statb) && S(st_size))
  189.                 do_mail(command,font,&xpos,&ypos);
  190.             else {
  191.                 set_icon(mbox_open);
  192.                 sleep(2);
  193.                 m_clearmode(M_ACTIVATE);
  194.             }
  195.             state &= ~PROCESSED;
  196.             update();
  197.             break;
  198.         case 'R':    /* screen is redrawn */
  199.             state &= ~PROCESSED;
  200.             get_size(&x,&y,&w,&h);
  201.             m_movecursor(x+30,0);
  202.             update();
  203.             break;
  204.         }
  205.     }
  206. }
  207.  
  208. /* run readmail in a subwindow */
  209.  
  210. do_mail(command,font,xp,yp)
  211. char *command;
  212. int font,*xp,*yp;
  213.     {
  214.     int xpos = *xp, ypos = *yp;
  215.     int code;
  216.     int n;
  217.     int x,y;
  218.  
  219.     alarm(0);
  220.     m_push(P_EVENT | P_FONT);
  221.     dprintf(stderr,"doing mail\n"); fflush(stderr);
  222.     n = m_makewindow(xpos, ypos, W_WIDE*cwide + 2*border,
  223.         W_HIGH*chigh + 2*border);
  224.     if (n==0) {    /* can't make window */
  225.         m_printstr("\007\fCan't open mail window, sorry");
  226.         m_pop();
  227.         return(0);
  228.         }
  229.     /*
  230.     if( *termcap  &&  newtermcap[0] == '\0' ) {
  231.         strcpy( newtermcap, get_termcap() );
  232.         termcap = newtermcap;
  233.     }
  234.     */
  235.       set_icon(mbox_zip);
  236.     m_selectwin(n);
  237.     m_font(font);
  238.     menu_load(1,MENU_COUNT,menu);
  239.     m_selectmenu(1);
  240.     m_printstr(MSG_READING);
  241.     m_ttyreset();
  242.     code = system(command);
  243.     m_printstr(MSG_CHECKING);
  244.     sleep(1);    /* for "New mail arrived" message */
  245.     dprintf(stderr,"Readmail completed code %d\n",code); fflush(stderr);
  246.     m_ttyset();
  247.  
  248.     /* see if window was moved - remember for next time */
  249.     
  250.     get_size(&x,&y,0,0);
  251.     if (abs(x-xpos) > 10 || abs(y-ypos) > 10) {
  252.         *xp = x;
  253.         *yp = y;
  254.         }
  255.     m_selectwin(0);
  256.     m_destroywin(n);
  257.     m_pop();
  258.     m_clearmode(M_ACTIVATE);
  259.     dprintf(stderr,"window deactivated\n"); fflush(stderr);
  260.     }
  261.  
  262. /* check the spool file for new mail and update message */
  263.  
  264. int
  265. update()
  266. {
  267.     alarm(0);
  268.     dprintf(stderr,"checking mail state 0x%x\n",state); fflush(stderr);
  269.     if (!stat(mail,&statb) && S(st_mtime)>S(st_atime) && S(st_size)) {
  270.         state &= ~PROCESSED;
  271.         if (S(st_mtime) != omtime) {
  272.         dprintf(stderr,"    First time New mail\n"); fflush(stderr);
  273.                         m_printstr("◆");
  274.               set_icon(mbox_full);
  275.             omtime = S(st_mtime);
  276.         }
  277.     }
  278.     else if (!(state&PROCESSED)) {
  279.         dprintf(stderr,"    Clearing new mail\n"); fflush(stderr);
  280.           set_icon(mbox_closed);
  281.         state |= PROCESSED;
  282.     }
  283.     alarm(poll);
  284. }
  285.  
  286. /*    Clean up and exit */
  287.  
  288. clean(n)
  289. int    n;
  290. {
  291.     m_ttyreset();
  292.     m_selectwin(0);
  293.     m_popall();
  294.     exit(n);
  295. }
  296.  
  297. usage(name,error)
  298. char *name, *error;
  299. {
  300.     fprintf(stderr,"Invalid flag: %s\n",error);
  301.     fprintf(stderr,
  302.         "usage: %s -[s|x<pos>|y<pos>|f<font>|p<poll>|M<mail_program>]\n"
  303.         ,name);
  304.     exit(1);
  305. }
  306.  
  307. /* down load an icon */
  308.  
  309. download_icon(icon,where)
  310. register struct icon *icon;    /* name of icon to download */
  311. int where;            /* bitmap to download icon to */
  312.    {
  313.  
  314.    int size;
  315.    int w_in=0, h_in=0;
  316.  
  317.    if (!local) {
  318.        /* first try the local machine */
  319.       dprintf(stderr,"looking for %s\n",icon->name);
  320.       m_bitfile(where, icon->name, &w_in, &h_in );
  321.       }
  322.  
  323.    if (h_in==0 || w_in==0) {    /* can't find icon */
  324.       dprintf(stderr,"Couldn't find %s, downloading\n",icon->name);
  325.       if (local_mode == -1)
  326.          ioctl(fileno(m_termout),TIOCLGET,&local_mode);
  327.       local_mode |= LLITOUT;
  328.       ioctl(fileno(m_termout),TIOCLSET,&local_mode);
  329.  
  330.       size = icon->h * (((icon->w+15)&~15)>>3);
  331.       m_bitldto(icon->w,icon->h,0,0,where,size);
  332.       m_flush();
  333.       write(fileno(m_termout),icon->data,size);
  334.       local_mode &= ~LLITOUT;
  335.       ioctl(fileno(m_termout),TIOCLSET,&local_mode);
  336.       }
  337.    else {
  338.       dprintf(stderr,"Found %s (%d x %d) expected %d x %d\n",
  339.                icon->name,w_in,h_in,icon->w,icon->h);
  340.       icon->w = w_in;
  341.       icon->h = h_in;
  342.       }
  343.    icon->type = where;
  344.    } 
  345.  
  346. set_icon(name)
  347. struct icon name;        /* name of icon */
  348.    {
  349.    int x0 = (w-name.w)/2;
  350.    int y0 = (h-name.h)/2;
  351.  
  352.    m_clear();
  353.    m_bitcopyto(x0,y0,name.w,name.h,0,0,0,name.type);
  354.    dprintf(stderr,"copy %s to %d,%d (%d x %d)from %d\n",
  355.            name.name,x0,y0,name.w,name.h,name.type);
  356.    m_flush();
  357.    }
  358.